﻿var menulist = {
    init: function () {
        $('#tableid').treegrid({
            fit: true,
            nowrap: false,
            border: false,
            rownumbers: true,
            animate: true,
            toolbar: toolbar,
            idField: 'MENU_ID',
            treeField: 'MENU_NAME',
            columns: [[
					{ title: lang.Menu_name, field: 'MENU_NAME', width: 230 },
            //{ title: '图标', field: 'MENU_IMG', width: 120, align: 'center' },
					{title: lang.type, field: 'MENU_TYPE', width: 100, align: 'center' },
                    { title: lang.connection_object, field: 'TARGET', width: 100, align: 'center' },
					{ title: lang.sequence, field: 'SORTCODE', width: 100, align: 'center' },
                    { title: lang.Link_address, field: 'NAVIGATEURL', width: 360 }
				]]
        });
        getmenu();
    },
    search: function () {
        getmenu();
    }
};
var toolbar = [{
    text: lang.add,
    iconCls: 'icon-add',
    handler: function () { openPage("menuedit", "", "", lang.add_menu); }
}, '-', {
    text: lang.modify,
    iconCls: 'icon-edit',
    handler: function () {
        var obj = $('#tableid').treegrid('getSelected');
        if (obj) {
            openPage("menuedit", "menuid=" + obj.MENU_ID, "", lang.modify_menu);
        }
        else {
            $.alert(lang.select_menu);
        }
    }
}, '-', {
    text: lang.delete_,
    iconCls: 'icon-remove',
    handler: function () {
        var obj = $('#tableid').treegrid('getSelected');
        if (obj) {
            var _menuid = obj.MENU_ID;
            var isParent = false;
            for (var i = 0; i < _menudata.length; i++) {
                if (_menudata[i].PARENTID == _menuid) {
                    isParent = true;
                }
            }
            if (isParent) { $.alert(lang.First_remove_the_subdirectories); }
            else {
                $.extend($.messager.defaults, {
                    ok: lang.ok,
                    cancel: lang.cancel
                });
                $.confirm(lang.Sure_to_delete_menu, lang.delete_, function (b) {
                    if (b) {
                        deletemenu(_menuid);
                    }
                });
            }
        }
        else {
            $.alert(lang.select_menu);
        }
    }
}];

var _menudata = [];
//#region 加载菜单数据
function getmenu() {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "QueryMenuList",
        success: function (data) {
            if (data && data.length > 0) {
                var _data = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.MENU_ID = data[i].MENU_ID;
                        obj.MENU_NAME = data[i].MENU_NAME;
                        obj.MENU_IMG = data[i].MENU_IMG;
                        obj.MENU_TYPE = data[i].MENU_TYPE;
                        obj.TARGET = data[i].TARGET;
                        obj.SORTCODE = data[i].SORTCODE;
                        obj.NAVIGATEURL = data[i].NAVIGATEURL;
                        getsubmenu(data, data[i].MENU_ID, obj);
                        _data.push(obj);
                    }
                }
                _menudata = data;
                $('#tableid').treegrid('loadData', _data);
            }

        }
    });

    function getsubmenu(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.MENU_ID = _d[i].MENU_ID;
                    obj.MENU_NAME = _d[i].MENU_NAME;
                    obj.MENU_IMG = _d[i].MENU_IMG;
                    obj.TARGET = _d[i].TARGET;
                    obj.SORTCODE = _d[i].SORTCODE;
                    obj.MENU_TYPE = _d[i].MENU_TYPE;
                    obj.NAVIGATEURL = _d[i].NAVIGATEURL;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubmenu(_d, _sdata[j].MENU_ID, _sdata[j]);
                }
            }
        }
    }
}

//#endregion

function deletemenu(menuid) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "EditMenuByMenuID",
        model: function () { return $.toJSON({ 'action': 0, 'MENU_ID': menuid }); },
        success: function (data) {
            $.alert(lang.Successfully_deleted, null, function () { menulist.search(); });
        }
    });
}